/*
 *  This file is part of "SchoolChoice",
 *  a school registration system.
 *
 *  Copyright (C) 2009 Nicolas Decerf, Olivier Jacquemin.
 *
 *  "SchoolChoice" is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  "SchoolChoice" is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program: see the file COPYING.txt.  If not, see
 *  http://www.gnu.org/licenses/.
 */

package be.schoolchoice.webapp.web;

import java.util.ArrayList;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import be.schoolchoice.webapp.data.PMF;
import be.schoolchoice.webapp.data.School;

/**
 * Web service providing operations on schools.
 */
public class SchoolsService {
    /**
     * Gets a list of schools corresponding to a search string.
     * Any School object containing the search string in one of its fields
     * is returned.
     * @param searchString The search string.
     * @return The list of School objects corresponding to the search string.
     */
    public static School[] search(String searchString) {
    	// TODO: implement correctly.
//    	ArrayList<School> result = new ArrayList<School>();
//    	result.add(new School("Ecole 1", "Rue 1", "1234", "Ville 1", 20));
//    	result.add(new School("Ecole 2", "Rue 2", "1234", "Ville 2", 30));
//		return result.toArray(new School[0]);

        PersistenceManager pm = PMF.get().getPersistenceManager();

        Query query = pm.newQuery(School.class);
        query.setFilter("postalCode == postalCodeParam");
        query.setOrdering("city desc");
        query.declareParameters("String postalCodeParam");

        School[] result;
        try {
            List<School> schools = (List<School>) query.execute(searchString);

	        // Explicit loop to avoid cast problem when using schools.toArray().
	        result = new School[schools.size()];
	        for (int i = 0; i < schools.size(); i += 1) {
	        	result[i] = schools.get(i);
	        }
        } finally {
            query.closeAll();
        }
		return result;
    }

    /**
     * The schools previously specified as preferred by the logged user.
     * @return Ordered (by decreasing preference level) list of School objects.
     * @see #setPreferredSchoolsIds(String[])
     */
    public static School[] getPreferredSchools() {
    	// TODO: implement correctly.
    	ArrayList<School> result = new ArrayList<School>();
    	result.add(new School("Ecole 1", "Rue 1", "1234", "Ville 1", 20));
    	result.add(new School("Ecole 2", "Rue 2", "1234", "Ville 2", 30));
		return result.toArray(new School[0]);
    }

    /**
     * Specifies which schools are preferred for the logged user.
     * @param sortedList Ordered (by decreasing preference level) list of school ids.
     * @see #getPreferredSchools()
     */
    public static void setPreferredSchoolsIds(String[] sortedList) {
    }

    /**
     * Creates a new school,
     * or changes the school with the same identifier if it exists already.
     * @param school The school to save.
     * @see #removeSchool(String)
     */
    public static void saveSchool(School school) {
        PersistenceManager pm = PMF.get().getPersistenceManager();
        try {
            pm.makePersistent(school);
        } finally {
            pm.close();
        }    	
    }

    /**
     * Deletes a school.
     * @param identifier The school id.
     * @see #saveSchool(School)
     */
    public static void removeSchool(String identifier) {
    }
}
